package com.tjbank.cssys.mybatis.impl;

import com.tjbank.cssys.mybatis.IamUserDao;
import com.tjbank.cssys.mybatis.entity.IamUser;
import com.tjbank.cssys.mybatis.entity.IamUserKey;
import com.tjbank.cssys.mybatis.mapper.IamUserMapper;
import com.tjbank.cssys.mybatis.sqlbuilder.IamUserSqlBuilder;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * IamUserDaoImpl
 * 
 * @author PG(Auto Generator)
 * @version V2.0
 */
@Service
public class IamUserDaoImpl implements IamUserDao {
    @Resource
    private IamUserMapper iamUserMapper;

    public IamUserDaoImpl() {
        super();
    }

    @Override
    public List<IamUser> selectListBySqlBuilderWithBLOBs(IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        return iamUserMapper.selectBySqlBuilderWithBLOBs(iamUserSqlBuilder);
    }

    @Override
    public List<IamUser> selectListBySqlBuilder(IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        return iamUserMapper.selectBySqlBuilder(iamUserSqlBuilder);
    }

    @Override
    public IamUser selectBySqlBuilderWithBLOBs(IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        List<IamUser> listIamUsers = selectListBySqlBuilderWithBLOBs(iamUserSqlBuilder);
        if (listIamUsers == null || listIamUsers.isEmpty()) {
            return null;
        } else if (listIamUsers.size() > 1) {
            throw new TooManyResultsException();
        }
        return listIamUsers.get(0);
    }

    @Override
    public IamUser selectBySqlBuilder(IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        List<IamUser> listIamUsers = selectListBySqlBuilder(iamUserSqlBuilder);
        if (listIamUsers == null || listIamUsers.isEmpty()) {
            return null;
        } else if (listIamUsers.size() > 1) {
            throw new TooManyResultsException();
        }
        return listIamUsers.get(0);
    }

    @Override
    public IamUser selectByPrimaryKey(IamUserKey iamUserKey) throws RuntimeException {
        return iamUserMapper.selectByPrimaryKey(iamUserKey);
    }

    @Override
    public long countBySqlBuilder(IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        return iamUserMapper.countBySqlBuilder(iamUserSqlBuilder);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int insert(IamUser iamUser) throws RuntimeException {
        return iamUserMapper.insert(iamUser);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int insertUseBatch(List<IamUser> listIamUsers) throws RuntimeException {
        return iamUserMapper.insertUseBatch(listIamUsers);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int updateBySqlBuilder(IamUser iamUser, IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        iamUserSqlBuilder.setUpdateObject(iamUser);
        return iamUserMapper.updateBySqlBuilder(iamUserSqlBuilder);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int updateBySqlBuilderSelective(IamUser iamUser, IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        iamUserSqlBuilder.setUpdateObject(iamUser);
        return iamUserMapper.updateBySqlBuilderSelective(iamUserSqlBuilder);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int updateByPrimaryKey(IamUser iamUser) throws RuntimeException {
        return iamUserMapper.updateByPrimaryKey(iamUser);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int updateByPrimaryKeySelective(IamUser iamUser) throws RuntimeException {
        return iamUserMapper.updateByPrimaryKeySelective(iamUser);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int deleteBySqlBuilder(IamUserSqlBuilder iamUserSqlBuilder) throws RuntimeException {
        return iamUserMapper.deleteBySqlBuilder(iamUserSqlBuilder);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
    public int deleteByPrimaryKey(IamUserKey iamUserKey) throws RuntimeException {
        return iamUserMapper.deleteByPrimaryKey(iamUserKey);
    }

    @Override
    public Map<String,String> getUserCode(Map<String,String> map) {
        Map<String,String> resultMap=new HashMap<>();
        int userCodeCount= iamUserMapper.selectUserCode(map);
        if(userCodeCount==0){
            resultMap.put("code","200");
            resultMap.put("message","不重复");
            return  resultMap;
        }
        resultMap.put("code","201");
        resultMap.put("message","重复");
        return  resultMap;
    }
}